package cn.fl.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cmm.mid.core.framework.page.Pager;
import cmm.user.facade.system.api.ISysOrgApiService;
import cmm.user.facade.system.dto.SysOrgDTO;
import cmm.web.core.framework.constant.SysOrgConstant;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.system.service.ISysFlowManageOrgRService;
import cn.fl.system.transform.SysFlowManageOrgRVOTransform;
import cn.fl.system.vo.SysFlowManageOrgRVO;
import fl.flow.facade.flow.api.IFlowApiService;
import fl.flow.facade.flow.dto.SysFlowManageOrgRDTO;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * @Description: 流程配置管理与组织机构关系 客户端服务层接口实现
 * @Description: 业务层方法名称前缀：新增：add* 删除：remove* 修改：modify* 批量：batch* 查询对象：select* get* 查询集合：search*
 */
@Service
public class SysFlowManageOrgRServiceImpl implements ISysFlowManageOrgRService {

	/**
	 * 日志
	 */
	@SuppressWarnings("unused")
	private static final Logger logger = LoggerFactory.getLogger(SysFlowManageOrgRServiceImpl.class);
	
	/**
	 * 流程 API 暴露服务
	 */
	@Autowired
	private IFlowApiService flowApiService;
	
	/**
	 * 组织架构 API 暴露服务
	 */
	@Autowired
	private ISysOrgApiService sysOrgApiService;

	@Override
	public SysFlowManageOrgRVO selectById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id))return null;
		//TODO 业务逻辑
		SysFlowManageOrgRDTO dto = null;//this.自定义ApiService.selectById(id);
		return SysFlowManageOrgRVOTransform.toVO(dto);
	}

	@Override
	public Long add(SysFlowManageOrgRVO entity) throws BaseException{
		if(null == entity)return null;
		//return this.sysFlowManageOrgRApiService.add(SysFlowManageOrgRVOTransform.toDTO(entity));
		//TODO 业务逻辑
		return null;
	}

	@Override
	public boolean removeByIds(List<Long> ids) throws BaseException{
		if(CollectionUtils.isEmpty(ids))return false;
		return this.flowApiService.removeFlowManageOrgRByIds(ids);
	}

	@Override
	public boolean modify(SysFlowManageOrgRVO entity) throws BaseException{
		if(null == entity)return false;
		//return this.sysFlowManageOrgRApiService.modify(SysFlowManageOrgRVOTransform.toDTO(entity));
		//TODO 业务逻辑
		return false;
	}

	@Override
	public Pager searchListPage(Pager page) throws BaseException{
		if(null == page)page = new Pager();
		return this.flowApiService.searchFlowManageOrgRByOrgCdListPage(page);
	}

	@Override
	public JSONObject searchOrgWithFlowRTree(Long manageId) {
		JSONObject result = new JSONObject();
		JSONArray tree = new JSONArray();
		List<String> orgCdList = new ArrayList<String>();
		if(StringUtils.isEmpty(manageId)){
			result.put("tree", tree);
			result.put("orgCdList", orgCdList);
			return result;
		}
		//先查询出来所有的关联关系
		List<SysFlowManageOrgRDTO> sysFlowManageOrgRDTOList = this.flowApiService.searchFlowManageOrgRListBy(manageId, CurrentThreadContext.getCurrentSysCd());
		if(CollectionUtils.isNotEmpty(sysFlowManageOrgRDTOList)){
			for (SysFlowManageOrgRDTO sysFlowManageOrgRDTO : sysFlowManageOrgRDTOList) {
				orgCdList.add(sysFlowManageOrgRDTO.getOrgCd());
			}
		}
		
		List<SysOrgDTO> sysOrgDTOList = this.sysOrgApiService.searchSysOrgListBy(CurrentThreadContext.getCurrentSysCd());
		if(CollectionUtils.isEmpty(sysOrgDTOList))return result;
		tree = this.getOrgTree(sysOrgDTOList, SysOrgConstant.SYS_ORG_CD_LVL_INIT, orgCdList);
		result.put("tree", tree);
		result.put("orgCdList", orgCdList);
		return result;
	}
	
	/**
	 * 
	 * @param list
	 * @param pid
	 * @return
	 */
	@SuppressWarnings("unchecked")
	private JSONArray getOrgTree(List<SysOrgDTO> list, Long pid, List<String> orgCdList){
		JSONArray tree = new JSONArray();
		JSONObject sysOrgObj = null;
		for (SysOrgDTO sysOrgDTO : list) {
			sysOrgObj = new JSONObject();
			//Long id = sysOrgDTO.getId();
			sysOrgObj.put("id", sysOrgDTO.getOrgCd());//当前节点CODE
			sysOrgObj.put("text", sysOrgDTO.getOrgNm());//组织架构全称
			sysOrgObj.put("pid", sysOrgDTO.getOrgPid());//组织架构pid
			
			if(orgCdList.contains(sysOrgDTO.getOrgCd())){
				sysOrgObj.put("ischecked", true);
			}else{
				sysOrgObj.put("ischecked", false);
			}
			
			if(sysOrgDTO.getOrgPid().equals(pid)){
				List<SysOrgDTO> childTree = this.getOrgTree(list, sysOrgDTO.getId(), orgCdList);
				if(CollectionUtils.isEmpty(childTree)){
					sysOrgObj.put("hasChildren",SysOrgConstant.SYS_ORG_ORG_CD_IS_HAV_CHILE_NO );//组织架构是否有子节点
				}else{
					sysOrgObj.put("children",childTree);
					sysOrgObj.put("hasChildren",SysOrgConstant.SYS_ORG_ORG_CD_IS_HAV_CHILE_YES);//组织架构是否有子节点
				}
				tree.add(sysOrgObj);
			}
		}
		return tree;
	}

	@Override
	public boolean modify(Long manageId, List<SysFlowManageOrgRVO> entityList) throws BaseException {
		if(StringUtils.isEmpty(manageId)){
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "流程主键不能为空!");
		}
		return this.flowApiService.modifyFlowManageOrgRBy(manageId, SysFlowManageOrgRVOTransform.toDTOList(entityList), CurrentThreadContext.getCurrentSysCd());
	}

	@Override
	public boolean modify(List<SysFlowManageOrgRVO> entityList) throws BaseException {
		if(CollectionUtils.isEmpty(entityList)){
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "集合不能为空!");
		}
		return this.flowApiService.modifyFlowManageOrgRBy(SysFlowManageOrgRVOTransform.toDTOList(entityList), CurrentThreadContext.getCurrentSysCd());
	}
}
